package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.PmsSkuVo;
import com.quan.mall.entity.PmsSku;
import com.quan.mall.mapper.PmsSkuMapper;
import com.quan.mall.service.PmsSkuService;


@Slf4j
@Service
public class PmsSkuServiceImpl extends MyBaseServiceImpl<PmsSkuMapper, PmsSku> implements PmsSkuService {

    @Autowired
	private PmsSkuMapper mapper;

	@Override
    public List<PmsSkuVo> queryList(PageUtils pageUtils, PmsSkuVo vo) {
        QueryWrapper<PmsSku> queryWrapper = new QueryWrapper<PmsSku>(vo);

        List<PmsSku> list = super.list(pageUtils, queryWrapper);
        List<PmsSkuVo> collect = list.stream().map(item -> {
            PmsSkuVo bean = new PmsSkuVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, PmsSkuVo vo) {
        QueryWrapper<PmsSku> queryWrapper = new QueryWrapper<PmsSku>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("id", vo.getKeyword())
                .or().like("skuName", vo.getKeyword());
            });
        }

        final IPage<PmsSku> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<PmsSku> records = page.getRecords();

        final List<PmsSku> collect = records.stream().map((item) -> {
            PmsSkuVo bean = new PmsSkuVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public void decrCount(Map<Long, Integer> decrMap) {
        for (Map.Entry<Long, Integer> entry : decrMap.entrySet()) {
            // 商品id
            Long skuId = Long.valueOf(entry.getKey());
            // 递减数量
            Object value = entry.getValue();
            Integer num = Integer.valueOf(value.toString());

            // 库存 >= 递减数量
            PmsSku sku = super.getById(skuId);
            if (sku.getStockNum() >= num) {
                this.baseMapper.decrCount(skuId, num);
            }
        }
    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        final PmsSku sku = super.getById(skuId);
        return sku.getSalePrice();
    }
}